<?php
class ControllerVoucherVoucher extends Controller
{
	private $error = array();
	
	public function index()
	{
		$this->id='content';
		$this->insert();
	}
	
	public function saveVoucher()
	{
		$this->data['savecomplete'] = false;
		$this->data['saveprintcomplete'] = false;
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			
			if($this->request->get['voucherid'] != "")
			{
				$this->request->post['voucherid'] = $this->request->get['voucherid'];
			}
			
			$this->data['voucherid'] = $this->request->post['voucherid'];
			
			if($this->request->post['voucherno'] == "auto")
			{
				$this->request->post['voucherno'] = $this->model_voucher_voucher->nextVoucherNo($this->request->post['vouchergroup']."_");
			}
			$this->request->post['voucherdate'] = $this->date->formatMySQLDateQuery($this->request->post['voucherdate'],"DMY","-");
			
			$this->request->post['totalpaid'] = $this->convertnumber($this->request->post['totalpaid']);
			$this->request->post['deptamount'] = $this->convertnumber($this->request->post['deptamount']);
			
			switch($this->request->post['vouchergroup'])
			{
				case "VEXSL":
				case "VEXEL":
				case "VEXCL":
				case "VINSL":
				case "VINEL":
				case "VINCL":
				break;
				default:
					$this->request->post['grandtotal'] = $this->convertnumber($this->request->post['totalpaid']);
					if($this->request->post['vouchertype'] == "EXPENSE")
					{
						$this->request->post['grandtotal'] = $this->convertnumber($this->request->post['totalpaid'])*(-1);
					}
					
				break;
			}
			
			
			
			$this->request->post['voucherid'] =  $this->model_voucher_voucher->save($this->request->post);
			$this->data['savecomplete'] = true;
		
		}
	}
	
	public function expense()
	{
		$this->load->model("voucher/voucher");
		
		$this->request->post['vouchertype'] = "EXPENSE";
		$this->data['form_title'] = $this->document->text['text_header_paymentvoucher'];
		
		$this->saveVoucher();
		
		$voucherid = $this->request->post['voucherid'];
		if($this->data['savecomplete'] == true)
		{
			if($this->request->post['print'] == "1")
			{
				$this->redirect('index.php?route=voucher/voucher/expense&voucherid='.$voucherid.'&print=1');
			}
			else
			{
				$this->redirect('index.php?route=voucher/voucher/expense&voucherid='.$voucherid);
			}

		}
				
		$this->getEXForm();
		$this->id='content';
		$this->template='voucher/voucher.tpl';
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function income()
	{
		$this->load->model("voucher/voucher");
		
		$this->request->post['vouchertype'] = "INCOME";
		$this->data['form_title'] = $this->document->text['text_header_receiptvoucher'];

		$this->saveVoucher();
		
		$voucherid = $this->request->post['voucherid'];
		if($this->data['savecomplete'] == true)
		{
			if($this->request->post['print'] == "1")
			{
				$this->redirect('index.php?route=voucher/voucher/income&voucherid='.$voucherid.'&print=1');
			}
			else
			{
				$this->redirect('index.php?route=voucher/voucher/income&voucherid='.$voucherid);
			}

		}
		
		
		$this->getEXForm();
		$this->id='content';
		$this->template='voucher/voucher.tpl';
		$this->layout="layout/function";
		
		$this->render();
	}
	
	
	
	private function getEXForm()
	{
		$this->load->model("core/user");
		$this->load->model("common/control");
		$this->load->model("voucher/voucher");
		
		$this->data['voucherid'] = $this->request->get['voucherid'];
		
		
		
		$this->data['error'] = @$this->error;
		
		if (!isset($this->request->get['orderid'])) {
			$this->data['action'] = $this->url->http('voucher/voucher/'.strtolower($this->request->post['vouchertype']));
		} else {
			$this->data['action'] = $this->url->http('voucher/voucher/'.strtolower($this->request->post['vouchertype']).'&orderid=' . $this->request->get['orderid']);
		}
		
		$this->data['cancel'] = $this->url->https('voucher/voucher/'.strtolower($this->request->post['vouchertype']));
		
		
		//Load data
		if ((isset($this->request->get['voucherid'])) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$this->data['voucher'] = $this->model_voucher_voucher->getItem($this->request->get['voucherid']);
    	}
		else
		{
			$this->data['voucher'] = $this->request->post;
		}
		
		$this->data['allowdelete'] = false;
		if($this->request->get['type'] == "edit" && $this->data['voucher']['orderid'] == "")
		{
			$this->data['allowdelete'] = true;
		}
		
		if($this->data['voucher']['voucherno'] == "")
		{
			$this->data['voucher']['voucherno'] = "auto";
		}
		
		//Dept amount
		$this->data['voucher']['deptamount'] = (float)$this->data['voucher']['deptamount'];
		
		//Voucher date
		if($this->data['voucher']['voucherdate'] == "") { $this->data['voucher']['voucherdate'] = date("Y-m-j H:m:s");}
		
		//Combobox Cashier
		$cashiers = $this->model_core_user->getList("AND usergroup = ''");
		$this->data['cb_cashier'] = $this->model_common_control->getDataCombobox($cashiers, "fullname", "userid", $this->data['voucher']['employeeid']);
	
		//Combobox Voucher Group/Account type
		$vouchergroups = array();
		if($this->request->post['vouchertype'] == "EXPENSE"){
			$vouchergroups = array(
				0 => array("vouchergroupid" => "VEXSP", "vouchergroupname" => "Payments to suppliers for goods and services (Ex: Purchase Order)"),
				1 => array("vouchergroupid" => "VEXCR", "vouchergroupname" => "Payments to customers for goods and services (Ex: Return order)"),
				2 => array("vouchergroupid" => "VEXES", "vouchergroupname" => "Salary"),
				3 => array("vouchergroupid" => "VEXEA", "vouchergroupname" => "Amortization (loss of intangible asset value over time)"),
				4 => array("vouchergroupid" => "VEXEO", "vouchergroupname" => "Office Operating cost / Purchase Assets (Ex: Electronic, Water, Buy assets for company using)"),
				5 => array("vouchergroupid" => "VEXSL", "vouchergroupname" => "DEBT/LOANS of partners"),
				6 => array("vouchergroupid" => "VEXCL", "vouchergroupname" => "DEBT/LOANS of customers"),
				7 => array("vouchergroupid" => "VEXEL", "vouchergroupname" => "DEBT/LOANS of employee"),
				8 => array("vouchergroupid" => "VEXSR", "vouchergroupname" => "RENT ASSETS or others from partners"),
				9 => array("vouchergroupid" => "VEXEM", "vouchergroupname" => "Marketing cost, public relationship"),
				10 => array("vouchergroupid" => "VEXCT", "vouchergroupname" => "Return the deposit for customer when they receive products"),
				11 => array("vouchergroupid" => "VEXCC", "vouchergroupname" => "Change money to customer"),
				12 => array("vouchergroupid" => "VEXEA", "vouchergroupname" => "Capital")
			);
		}else
		{
			$vouchergroups = array(
				0 => array("vouchergroupid" => "VINCS", "vouchergroupname" => "Receipts from the sale of goods or services (Ex: Sale Order, Sale services )"),
				1 => array("vouchergroupid" => "VINEI", "vouchergroupname" => "Receipts from internal company (employee)"),
				2 => array("vouchergroupid" => "VINSR", "vouchergroupname" => "Receipts from the returning products to supplier"),
				3 => array("vouchergroupid" => "VINSL", "vouchergroupname" => "DPBT/LOANS of Suppliers"),
				4 => array("vouchergroupid" => "VINCL", "vouchergroupname" => "DPBT/LOANS of Customer"),
				5 => array("vouchergroupid" => "VINEL", "vouchergroupname" => "DPBT/LOANS of Employee"),
				6 => array("vouchergroupid" => "VINCD", "vouchergroupname" => "Receipts from customer deposit order/products"),
				7 => array("vouchergroupid" => "VINCH", "vouchergroupname" => "Receipts from customer change products"),
				8 => array("vouchergroupid" => "VINCO", "vouchergroupname" => "Receipts from customer for other activities"),
				9 => array("vouchergroupid" => "VINSC", "vouchergroupname" => "Supplier change money"),
				10 => array("vouchergroupid" => "VINEA", "vouchergroupname" => "Capital")
			);
		}
		
		
		$this->data['cb_vouchergroup'] = $this->model_common_control->getDataCombobox($vouchergroups, "vouchergroupname", "vouchergroupid", $this->data['voucher']['vouchergroup']);
		
		//Combobox Partner
		switch(substr($this->data['voucher']['vouchergroup'],3,1))
		{
			case "S":
				$users = $this->model_core_user->getList("AND usergroup = 'supplier'");
			break;
			case "C":
				$users = $this->model_core_user->getList("AND usergroup = 'customer'");
			break;
			case "E": 
				$users = $this->model_core_user->getList("AND usergroup = ''");
			break;
			default:			
				if($this->request->post['vouchertype'] == "EXPENSE")
				{
					$users = $this->model_core_user->getList("AND usergroup = 'supplier'");
				}
				else
				{
					$users = $this->model_core_user->getList("AND usergroup = 'customer'");
				}
			break;
		}
		
		$this->data['cb_partner'] = $this->model_common_control->getDataCombobox($users, "fullname", "userid", $this->data['voucher']['partnerid']);
		
		//Combobox Payment type
		$paymenttypes = array(
			0 => array("paymenttype" => "cash"),
			1 => array("paymenttype" => "bank")
		);
		
		$this->data['cb_paymenttype'] = $this->model_common_control->getDataCombobox($paymenttypes, "paymenttype", "paymenttype", $this->data['voucher']['paymenttype']);	
		
	}
	
	public function cbobject()
	{
		$this->load->model("core/user");
		$this->load->model("common/control");
		
		$vouchergroupid = $this->request->get['groupid'];
		
		$users = array();
		switch(substr($vouchergroupid,3,1))
		{
			case "S":
				$users = $this->model_core_user->getList("AND usergroup = 'supplier'");
			break;
			case "C":
				$users = $this->model_core_user->getList("AND usergroup = 'customer'");
			break;
			case "E": 
				$users = $this->model_core_user->getList("AND usergroup = ''");
			break;
			default:
			break;
		}
			
    	$this->data['output'] = $this->model_common_control->getDataCombobox($users, "fullname", "userid", "");
	
    	//$this->getList();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function partnerinfo()
	{
		$this->load->model("core/user");
		$this->load->model("voucher/voucher");
		
		$partnerid = $this->request->get['partnerid'];
			
    	$this->data['user'] = $this->model_core_user->getItem($partnerid);
		$this->data['user']['dept'] = $this->model_voucher_voucher->getDeptFromPartner($partnerid);
	
    	//$this->getList();
		$this->id="content";
		$this->template="voucher/voucherpartner.tpl";
		$this->render();
	}
	
	public function insert()
	{
		$this->load->model("voucher/order");
		$this->load->model("voucher/ordertemp");
		
		$orderid = $this->model_voucher_ordertemp->insertdefault("IMS");
		$this->redirect('index.php?route=voucher/purchase/update&orderid='.$orderid);
		//$this->redirect($this->url->http('voucher/purchase/update'));
	}
	
	public function update() {
		$this->load->model("voucher/order");
		$this->load->model("voucher/orderdetail");
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		
		$orderid = $this->request->get['orderid'];
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			$this->_saveordertemp();
			$ordertemp = $this->model_voucher_ordertemp->getItem($orderid);
			$this->model_voucher_order->saveorder($ordertemp);
			$orderdetails = $this->model_voucher_orderdetailtemp->getList(" AND orderid = '".$orderid."'");
			foreach($orderdetails as $item)
			{
				$this->model_voucher_orderdetail->saveorderdetail($item);
			}
			$this->redirect('index.php?route=voucher/purchase/update&orderid='.$orderid);
		}
    
    	$this->getForm();
  	}

	public function edit() {
		$this->load->model("voucher/order");
		$this->load->model("voucher/orderdetail");
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		
		$orderid = $this->request->get['orderid'];
    	$ordersaved = $this->model_voucher_order->getItem($orderid);
		if(count($ordersaved) > 0)
		{
			$this->model_voucher_ordertemp->saveorder($ordersaved);
			$this->model_voucher_orderdetailtemp->deleteorder($orderid);
			$orderdetails = $this->model_voucher_orderdetail->getList(" AND orderid = '".$orderid."'");
			foreach($orderdetails as $item)
			{
				$this->model_voucher_orderdetailtemp->saveorderdetail($item);
			}
		}
    	$this->redirect('index.php?route=voucher/purchase/update&orderid='.$orderid);
  	}
	
	
	
	private function convertnumber($str)
	{
		$str = str_replace("," ,"",$str);
		return (float)$str;
	}
	
	private function getForm()
	{
		$this->load->model("core/user");
		$this->load->model("common/control");
		$this->load->model("category/stock");
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		
		$this->data['orderid'] = $this->request->get['orderid'];
		
		
		$this->data['error'] = @$this->error;
		
		if (!isset($this->request->get['orderid'])) {
			$this->data['action'] = $this->url->http('voucher/purchase/insert');
		} else {
			$this->data['action'] = $this->url->http('voucher/purchase/update&orderid=' . $this->request->get['orderid']);
		}
		
		$this->data['cancel'] = $this->url->https('voucher/purchase');
	
	
		
		$stocks = $this->model_category_stock->getList();
		
		
		
		if ((isset($this->request->get['orderid'])) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$this->data['order'] = $this->model_voucher_ordertemp->getItem($this->request->get['orderid']);
			$this->data['orderdetails'] = $this->model_voucher_orderdetailtemp->getList(" AND orderid = '".$this->request->get['orderid']."'");
			for($i=0; $i < count($this->data['orderdetails']); $i++)
			{
				$this->data['orderdetails'][$i]['cb_stocks'] = $this->model_common_control->getDataCombobox($stocks, "stockname", "stockid", ""); 
			}
    	}
		else
		{
			$this->data['order'] = $this->request->post;
		}
		
		//Set default values
		if($this->data['order']['ordernumber'] == "") {$this->data['order']['ordernumber'] = "auto";}
		if($this->data['order']['orderdate'] == "") {$this->data['order']['orderdate'] = date("Y-m-j H:m:s");}
		
		
		$paymenttypes = array(
			0 => array("paymenttype" => "cash"),
			1 => array("paymenttype" => "bank")
		);
		
		$suppliers = $this->model_core_user->getList("AND usergroup = 'supplier'");
		$users = $this->model_core_user->getList("AND usergroup = ''");
		
		$this->data['cb_suppliers'] = $this->model_common_control->getDataCombobox($suppliers, "fullname", "userid", $this->data['order']['partnerid']);
		$this->data['cb_users'] = $this->model_common_control->getDataCombobox($users, "fullname", "userid", $this->data['order']['employeeid']);
		
		$this->data['cb_paymenttypes'] = $this->model_common_control->getDataCombobox($paymenttypes, "paymenttype", "paymenttype", $this->data['order']['paymenttype']);
		
		$this->id='content';
		$this->template='voucher/purchase.tpl';
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function delete() {
		//$this->load->language('core/site');
		//$this->data = array_merge($this->data, $this->language->getData());
		
		//$this->document->title = $this->language->get('heading_title');
		$this->load->model("voucher/orderdetailtemp");
			
    	if (isset($this->request->post['item'])) 
		{
			foreach ($this->request->post['item'] as $orderdetailid) {
				$this->model_voucher_orderdetailtemp->delete($orderdetailid);
			}
			$this->data['output'] = "Delete successfully";
    	}
	
    	//$this->getList();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function validateForm()
	{	
		return TRUE;
		
		/*if (strlen($this->request->post['stockname']) == 0) {
      		$this->error['stockname'] = "Stock name cannot be null";
    	}

		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}*/
	}
	
	
}
?>